/**
 * 跨平台视频通话测试脚本
 * 用于测试PC端和Taro端之间的视频通话功能
 */

const io = require('socket.io-client')

// 测试配置
const API_BASE = 'http://localhost:3000'
const DOCTOR_ID = 'doctor_123' // 模拟医生ID
const PATIENT_ID = 'patient_456' // 模拟患者ID

// 创建Socket连接
const doctorSocket = io(API_BASE, { query: { userId: DOCTOR_ID } })
const patientSocket = io(API_BASE, { query: { userId: PATIENT_ID } })

console.log('🚀 开始跨平台视频通话测试...\n')

// 医生端事件处理
doctorSocket.on('connect', () => {
  console.log('✅ 医生端连接成功')
})

doctorSocket.on('video:incoming', (payload) => {
  console.log('📞 医生端收到视频通话邀请:', payload)
  console.log('   邀请者:', payload.fromName || '未知')
  console.log('   通话ID:', payload.callId)
  
  // 模拟医生接听
  setTimeout(() => {
    console.log('👨‍⚕️ 医生接听视频通话')
    // 这里可以添加接听逻辑
  }, 2000)
})

doctorSocket.on('video:offer', (payload) => {
  console.log('📥 医生端收到offer:', payload.callId)
})

doctorSocket.on('video:answer', (payload) => {
  console.log('📥 医生端收到answer:', payload.callId)
})

doctorSocket.on('video:ice-candidate', (payload) => {
  console.log('📥 医生端收到ICE候选:', payload.callId)
})

doctorSocket.on('video:end', (payload) => {
  console.log('📞 医生端收到结束通话信号:', payload.callId)
})

doctorSocket.on('video:reject', (payload) => {
  console.log('❌ 医生端收到拒绝信号:', payload.callId)
})

// 患者端事件处理
patientSocket.on('connect', () => {
  console.log('✅ 患者端连接成功')
})

patientSocket.on('video:incoming', (payload) => {
  console.log('📞 患者端收到视频通话邀请:', payload)
  console.log('   邀请者:', payload.fromName || '未知')
  console.log('   通话ID:', payload.callId)
  
  // 模拟患者接听
  setTimeout(() => {
    console.log('👤 患者接听视频通话')
    // 这里可以添加接听逻辑
  }, 2000)
})

patientSocket.on('video:offer', (payload) => {
  console.log('📥 患者端收到offer:', payload.callId)
})

patientSocket.on('video:answer', (payload) => {
  console.log('📥 患者端收到answer:', payload.callId)
})

patientSocket.on('video:ice-candidate', (payload) => {
  console.log('📥 患者端收到ICE候选:', payload.callId)
})

patientSocket.on('video:end', (payload) => {
  console.log('📞 患者端收到结束通话信号:', payload.callId)
})

patientSocket.on('video:reject', (payload) => {
  console.log('❌ 患者端收到拒绝信号:', payload.callId)
})

// 测试函数
function testDoctorToPatient() {
  console.log('\n🧪 测试：医生发起视频通话给患者')
  
  const callId = `video_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
  
  doctorSocket.emit('video:forward', {
    to: PATIENT_ID,
    callId: callId,
    from: DOCTOR_ID,
    fromName: '张医生'
  })
  
  console.log('📤 医生发送视频通话邀请:', {
    to: PATIENT_ID,
    callId: callId,
    from: DOCTOR_ID,
    fromName: '张医生'
  })
}

function testPatientToDoctor() {
  console.log('\n🧪 测试：患者发起视频通话给医生')
  
  const callId = `video_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
  
  patientSocket.emit('video:forward', {
    to: DOCTOR_ID,
    callId: callId,
    from: PATIENT_ID,
    fromName: '李患者'
  })
  
  console.log('📤 患者发送视频通话邀请:', {
    to: DOCTOR_ID,
    callId: callId,
    from: PATIENT_ID,
    fromName: '李患者'
  })
}

function testWebRTCSignaling() {
  console.log('\n🧪 测试：WebRTC信令交换')
  
  const callId = `video_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
  
  // 模拟offer
  const offer = {
    type: 'offer',
    sdp: 'mock-sdp-offer'
  }
  
  doctorSocket.emit('video:offer', {
    callId: callId,
    offer: offer,
    to: PATIENT_ID
  })
  
  console.log('📤 医生发送offer:', callId)
  
  // 模拟answer
  setTimeout(() => {
    const answer = {
      type: 'answer',
      sdp: 'mock-sdp-answer'
    }
    
    patientSocket.emit('video:answer', {
      callId: callId,
      answer: answer,
      to: DOCTOR_ID
    })
    
    console.log('📤 患者发送answer:', callId)
  }, 1000)
  
  // 模拟ICE候选
  setTimeout(() => {
    const candidate = {
      candidate: 'mock-ice-candidate',
      sdpMLineIndex: 0,
      sdpMid: '0'
    }
    
    doctorSocket.emit('video:ice-candidate', {
      callId: callId,
      candidate: candidate,
      to: PATIENT_ID
    })
    
    console.log('📤 医生发送ICE候选:', callId)
  }, 2000)
}

function testEndCall() {
  console.log('\n🧪 测试：结束视频通话')
  
  const callId = `video_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
  
  doctorSocket.emit('video:end', {
    callId: callId,
    to: PATIENT_ID
  })
  
  console.log('📤 医生结束视频通话:', callId)
}

function testRejectCall() {
  console.log('\n🧪 测试：拒绝视频通话')
  
  const callId = `video_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
  
  patientSocket.emit('video:reject', {
    callId: callId,
    to: DOCTOR_ID
  })
  
  console.log('📤 患者拒绝视频通话:', callId)
}

// 运行测试
setTimeout(() => {
  testDoctorToPatient()
}, 3000)

setTimeout(() => {
  testPatientToDoctor()
}, 6000)

setTimeout(() => {
  testWebRTCSignaling()
}, 9000)

setTimeout(() => {
  testEndCall()
}, 12000)

setTimeout(() => {
  testRejectCall()
}, 15000)

// 清理函数
process.on('SIGINT', () => {
  console.log('\n🛑 测试结束，断开连接...')
  doctorSocket.disconnect()
  patientSocket.disconnect()
  process.exit(0)
})

console.log('⏰ 测试将在3秒后开始...')
console.log('💡 按 Ctrl+C 结束测试')
